<template>
  <div class="body">
    <div class="title" style="padding-left: 10px">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        style="width: 100%"
      >
        <el-form-item label="学科名称">
          <el-input v-model="formInline.subjectName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click=" formInline.subjectName = ''">清除</el-button>
          <el-button type="primary" @click="onSearch">搜索</el-button>
        </el-form-item>
        <el-form-item style="float: right; margin-right: 0">
          <el-button
            type="success"
            icon="el-icon-edit"
            @click="dialogFormVisible = true"
            >新增学科</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="alert" style="margin-top: 0">
      <i class="el-icon-info"></i> 数据一共 {{counts}} 条
    </div>
    <el-table
      :data="tableData"
      highlight-current-row
      style="width: 100%"
      :header-cell-style="{
        background: '#fafafa',
        color: '#909399',
        borderBottom: '2px #ccc solid',
      }"
      fit
    >
      <el-table-column prop="index" label="序号" type="index" width="90" />
      <el-table-column prop="subjectName" label="学科名称" />
      <el-table-column prop="username" label="创建者" />
      <el-table-column label="创建日期" width="150" >
        <template slot-scope="scope">
         <span>{{ disponseTime(scope.row.addDate) }}</span>
         <!-- <span>{{ scope.row.addDate}}</span> -->
        </template>
      </el-table-column>
      <el-table-column  label="前台是否显示"  >
         <!-- 用自定义插槽并用三元表达式判断是否 -->
        <template slot-scope="scope">
          <span>{{ scope.row.isFrontDisplay   ? '是': '否'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="二级目录"  />
      <el-table-column prop="tags" label="标签" />
      <el-table-column prop="totals" label="题目数量" />
      <el-table-column prop="address" label="操作" width="230">
        <template slot-scope="{ row }">
          <el-button type="text" @click="$router.push(`/subjects/directorys?id=${row.id}&name=${row.subjectName}`)">学科分类</el-button>
          <el-button type="text" @click="$router.push(`/subjects/tags?id=${row.id}&name=${row.subjectName}`)">学科标签</el-button>
          <el-button type="text" @click="changeRole(row.id)">修改</el-button>
          <el-button type="text" @click="deleteRole(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      style="text-align :right ; margin-top: 18px"
      @size-change="handleSizeChange"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="pages.pagesize"
      :current-page="pages.page"
      @current-change="handleCurrentChange"
      layout="  prev, pager,next, sizes, jumper"
      :total="counts"
       >
    </el-pagination>

    <el-dialog  :title="this.form.id ? '编辑学科': '新增学科'" :visible.sync="dialogFormVisible" width="400px">
      <el-form ref="xiaomi" :inline="true" :model="form" :rules="rules" style="margin-bottom : 10px">
        <el-form-item label="学科名称" label-width="100px" prop="subjectName">
          <el-input v-model="form.subjectName" autocomplete="off"  placeholder="请输入学科名称"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" label-width="100px">
          <el-switch
            style="margin-left: 10px"
            v-model="value"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changeFn"

           >
      </el-switch>
           </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" >取 消</el-button>
        <el-button type="primary" @click="submit"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, searchName, remove, detail, update, add } from '@/api/hmmm/subjects.js'
export default {
  name: 'subjects',
  data () {
    return {
      pages: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海'
        }

      ],
      formInline: {
        subjectName: ''
      },
      form: { subjectName: '', isFrontDisplay: '' },
      rules: { subjectName: [{ required: true, message: '请输入学科名称' }] },
      dialogFormVisible: false,
      value: '',

      currentPage1: 2,
      counts: 0
    }
  },
  created () {
    this.list()
  },
  methods: {
    changeFn (value) {
      console.log(value)
      if (value) {
        this.form.isFrontDisplay = 1
      } else {
        this.form.isFrontDisplay = 0
      }

      // console.log(this.form.value)
    },
    // 渲染列表
    async list () {
      const res = await list(this.pages)
      // console.log(res)
      this.tableData = res.data.items
      this.counts = res.data.counts
    },
    // 处理时间格式
    disponseTime (str) {
      return str?.replace(/T/, ' ').replace(/.000Z/, '')
    },
    // 搜索框
    async onSearch () {
      this.pages = {
        page: 1,
        pagesize: 10,
        ...this.formInline
      }
      const { data } = await searchName({ ...this.pages })
      this.tableData = data.items
    },
    // 点击一页几条数据把pages.pagesizes的值赋给点击切换的页数
    handleSizeChange (val) {
      this.pages.pagesize = val
      // 重新渲染数据
      this.list()
    },
    // 点击第几页把pages.page的值赋给点击的页数
    handleCurrentChange (val) {
      this.pages.page = val
      // 重新渲染数据
      this.list()
    },
    // 删除功能
    async deleteRole (id) {
      try {
        await this.$confirm('确定要删除吗?')
        await remove(id)
        this.list()
      } catch (err) {
        console.log(err)
      }
    },
    // 修改功能  编辑具体某一个数据的信息
    async changeRole (id) {
      const { data } = await detail(id)
      // console.log(data)
      this.form = data
      this.form.subjectName = data.subjectName
      this.form.isFrontDisplay = data.isFrontDisplay
      console.log(this.form.value)
      this.dialogFormVisible = true
    },
    cancel () {
      // 手动清空表单
      this.form = {
        subjectName: ''
      }
      this.$refs.xiaomi.resetFields() // 移除校验
      this.dialogFormVisible = false
    },
    // 确定按钮
    async submit () {
      console.log(this.value)
      try {
        await this.$refs.xiaomi.validate()
        // 只有校验通过的通过下，才会执行await的下方内容
        if (this.form.id) {
          await update(this.form)
        } else {
          await add(this.form)
        }

        // 重新渲染数据
        this.list()
        this.$message.success('操作成功了')
        this.cancel()
        this.dialogFormVisible = false
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>

<style scoped lang="less">
.body {
  box-sizing: border-box;
  padding: 20px;
  max-width: 100%;
  margin: 10px;
  background-color: #fff;
  color: #303133;
  .alert {
    height: 35px;
    width: 100%;
    padding-left: 16px;
    margin: 15px 0;
    border-radius: 3px;
    background-color: #f4f4f4;
    line-height: 35px;
    color: #909399;
    font-size: 13px;

  }
}
</style>
